package com.yoyo.yyadsdk.util;

import android.text.TextUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.yoyo.yyadsdk.util.StringUtils.isNull;

/**
 * 验证工具类
 */
public class ValidateUtil {

    private static final int PASS_LOW_LIMIT = 6;
    private static final int PASS_HIGH_LIMIT = 16;

    /**
     * 验证手机号码是否合法
     */
    public static boolean validatePhoneNumber(String mobiles) {
        String telRegex = "^((13[0-9])|(15[^4])|(18[0-9])|(17[0-8])|(147,145))\\d{8}$";
        return !TextUtils.isEmpty(mobiles) && mobiles.matches(telRegex);
    }

    /**
     * 验证身份证号码是否合法
     */
    public static boolean validateIDCardNumber(String number) {
        return (number.length() == 15 && number.matches("^\\d{15}"))
                || (number.length() == 18 && (number.matches("^\\d{17}[x,X,\\d]")));
    }

    /**
     * 判断是不是英文字母
     */
    public static boolean isECharacter(String codePoint) {
        return codePoint.matches("^[A-Za-z]$");
    }

    /**
     * 判断是否是颜色值
     */
    public static Boolean isColor(String str) {
        return !isNull(str) && str.matches("^#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})$");
    }

    /**
     * 判断是否是图片
     */
    public static Boolean isPicture(String str) {
        return !isNull(str) && str.matches("^([hH][tT]{2}[pP]://|[hH][tT]{2}[pP][sS]://).*(\\.jpg|\\.png|\\.PNG|\\.JPG|\\.jpeg|\\.JPEG)$");
    }

    /**
     * 判断是否是链接
     */
    public static Boolean isUrl(String str) {
        return !isNull(str) && str.matches("^([hH][tT]{2}[pP]://|[hH][tT]{2}[pP][sS]://).*$");
    }

    /**
     * 判断是否是邮箱
     */
    public static boolean isEmail(String email) {
        String str = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(email);
        return !TextUtils.isEmpty(email) && m.matches();
    }
}